//
//  TWBNetworkTools.swift
//  weiBo
//
//  Created by 杨雷 on 2016/12/12.
//  Copyright © 2016年 Yang.L. All rights reserved.
//

import UIKit
import AFNetworking

enum RequestType {
    case get
    case post
}



class TWBNetworkTools: AFHTTPSessionManager {
    
    typealias callBack = (Any?, Error?) -> ()
    
    static let sharedTools: TWBNetworkTools = {
        
        let tools = TWBNetworkTools ()
        
        tools.responseSerializer.acceptableContentTypes?.insert("text/html")
        
        tools.responseSerializer.acceptableContentTypes?.insert("text/plain")
        
        return tools
    }()
    
    func request(type: RequestType, url: String, params: Any?, callBack: @escaping callBack) {
        
        if type == .get {
            
            get(url, parameters: params, progress: nil, success: { (_, response) in
                
                callBack(response, nil)
                
            }, failure: { (_, error) in
                
                callBack(nil, error)
                
            })
            
        } else {
            
            post(url, parameters: params, progress: nil, success: { (_, response) in
                
                callBack(response, nil)
                
            }, failure: { (_, error) in
                
                callBack(nil, error)
                
            })
            
        }
        
    }
    
}

//extension TWBNetworkTools {
//    
//    func upload(status: String, image: UIImage, callBake: @escaping callBack) {
//        
//        let url = "https://upload.api.weibo.com/2/statuses/upload.json"
//        
//        let params = [
//            
//            "access_token": TWBUserAccountViewModel.sharedUserAccount.accessToken!,
//            
//            "status": status
//        ]
//        
//        let imageData = UIImageJPEGRepresentation(image, 0.5)
//        
//        post(url, parameters: params, constructingBodyWith: { (formData)-> Void in
//            
//            formData.appendPart(withFileData: imageData!, name: "pic", fileName: "test", mimeType: "image/jpeg")
//            
//        }, progress: nil, success: {(_, response) -> Void in
//            
//            callBack(response, nil)
//            
//        }, failure: {(_, error) -> Void in
//        
//            callBack(nil, error)
//        })
//    }
//}

extension TWBNetworkTools {
    
    func requestUserInfo (accessToken: String, uid: Int64, callBake: @escaping callBack) {
        
        let url = "https://api.weibo.com/2/users/show.json"
        
        let params = [
            
            "access_token": accessToken,
            
            "uid" : uid
        ] as [String: Any]
        
        request(type: .get, url: url, params: params, callBack: callBake)
        
    }
    
    func requestAccessToken(code: String, callBack: @escaping callBack) {
        
        let url = "https://api.weibo.com/oauth2/access_token"
        
        let params = [
        
            "client_id" : AppKey,
            
            "client_secret" : AppSecret,
            
            "grant_type" : "authorization_code",
            
            "code" : code,
            
            "redirect_uri" : Redirect_uri

        ]
        
        request(type: .post, url: url, params: params, callBack: callBack)
        
    }
}

extension TWBNetworkTools {
    
    func requestStatus(callBack: @escaping callBack) {
        
        let url = "https://api.weibo.com/2/statuses/friends_timeline.json"
        
        let params = [
        
            "access_token" : TWBUserAccountViewModel.sharedUserAccount.accessToken!
        
        ]
        
        request(type: .get, url: url, params: params, callBack: callBack)
        
        
    }
    
    
}
